package nbcp.web.info

import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import nbcp.base.comm.*
import nbcp.base.extend.*
import nbcp.db.mongo.entity.SysCorporation
import org.springframework.web.bind.annotation.*
import nbcp.base.utils.MyUtil
import nbcp.comm.JsonpMapping
import nbcp.db.*
import nbcp.db.mongo.*
import nbcp.db.sys.*
import javax.servlet.http.HttpServletRequest
import org.springframework.data.mongodb.core.query.Query as CoreQuery;
import org.springframework.data.mongodb.core.query.Update as CoreUpdate


/**
 * Created by udi on 17-3-28.
 * 晋京
 */

@Api(description = "企业信息", tags = arrayOf("CorpInfo"))
@RestController
@JsonpMapping("/corpInfo")
class CorpInfo_Controller {

    //日志
    companion object {
        //private val logger = LoggerFactory.getLogger(CorpInfo_Controller::class.java)
    }

    @ApiOperation(value = "保存企业信息，没有图片信息")
    @JsonpMapping("/save")
    fun save(
            @JsonModel corpInfo: SysCorporation, //更改的是ModifySysCorporation表
            session: HttpServletRequest
    ): JsonResult {

        var map = hashMapOf<String, String>();
        map["name"] = "企业名称不能为空";
        map["address"] = "企业地址不能为空";

        var msg = MyUtil.checkEmpty(corpInfo, map);
        if (msg.HasValue) {
            return JsonResult(msg);
        }
        var city = IntCodeName();
        if( corpInfo.city.code > 0) {
            var fullName = mor.system.sysCity.query(mor.system.sysCity.code match corpInfo.city.code).toEntity()!!.fullName;//"code"
            city = IntCodeName(corpInfo.city.code, fullName);
        }

        //正式系统可能在modify里查不到信息，此时插入一条记录
        var modifyCorp = mor.system.sysCorporation.queryById(session.Corp.id).toEntity()
        if (modifyCorp == null) {
            return JsonResult("找不到企业修改信息表")
        }


        mor.system.sysCorporation.updateById(session.Corp.id)
                .set(mor.system.sysCorporation.address, corpInfo.address)
                .set(mor.system.sysCorporation.phone, corpInfo.phone)
                .set(mor.system.sysCorporation.weixin, corpInfo.weixin)
                .set(mor.system.sysCorporation.detail, corpInfo.detail)
                .set(mor.system.sysCorporation.city.toString(), city)
                .set(mor.system.sysCorporation.name, corpInfo.name)
//                .set(mor.system.modifySysCorporation.validate, false)
                .exec();

        return JsonResult()
    }

    @ApiOperation(value = "获取已审核通过的企业信息")
    @JsonpMapping("/getInfo")
    fun getInfo(session: HttpServletRequest): ApiResult<SysCorporation> {
        var ret = ApiResult<SysCorporation>();
        var n = mor.system.sysCorporation.queryById(session.Corp.id).toEntity();
        if (n == null) {
            return ApiResult("找不到企业信息")
        }
        ret.data = n
        //加日志
//        if (ret.data == null) {
//            logger.error("企业id为" + session.CorpId + ",未找到相关企业信息")
//        }
//        ret.data!!.menus.clear();
        return ret;
    }

    @ApiOperation("设置企业Logo")
    @JsonpMapping("/setLogo")
    fun setLogo(
            session: HttpServletRequest,
            logoId: String,
            logoUrl: String
    ): JsonResult {

        mor.system.sysCorporation.updateById(session.Corp.id)
                .set(mor.system.sysCorporation.logo.toString(), IdUrl(logoId, logoUrl))
                .exec();

        if (mor.affectRowCount == 0) {
            return JsonResult("设置企业Logo失败")
        }
        return JsonResult()
    }
}
